﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;
using HCFramework.API.LINQ;
using System.Data;

namespace HCFramework.Product.Admin.ManufacturerManage
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void RadGridMain_PreRender(object sender, EventArgs e)
        {
            LoadGridData();
        }
        public void LoadGridData()
        {
            HCDataContext db = new HCDataContext();
            var result = from a in db.Manufacturers
                         select a;
            RadGridMain.DataSource = result;
            RadGridMain.DataBind();
        }
        protected void RadGridMain_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
        {
            if (e.CommandName == "Them")
            {
                TextBox txtName = e.Item.FindControl("txtName") as TextBox;
                TextBox txtWebsite = e.Item.FindControl("txtWebsite") as TextBox;
                TextBox txtOrderDisplay = e.Item.FindControl("txtOrderDisplay") as TextBox;
                FileUpload fileUploadLogoUrl = e.Item.FindControl("fileUploadLogoUrl") as FileUpload;

                Manufacturer m = new Manufacturer();
                m.LogoUrl = fileUploadLogoUrl.HasFile ? M.FileM.UploadAndReturnURL(fileUploadLogoUrl, "/Product/Admin/ManufacturerManage/Image/") : "";
                m.Name = txtName.Text;
                m.OrderDisplay = int.Parse(txtOrderDisplay.Text);
                m.Website = txtWebsite.Text;
                C.ManufacturerController.Insert(m);
            }
        }

        protected void LinkButtonDelete_Click(object sender, EventArgs e)
        {
            foreach (GridItem gridItem in RadGridMain.SelectedItems)
            {
                GridDataItem dataItem = (GridDataItem)gridItem;
                int id = int.Parse(dataItem["ManufacturerID"].Text);

                C.ManufacturerController.Delete(id);
            }
        }

        protected void LinkButtonUpdate_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in RadGridMain.Items)
            {
                int id = int.Parse(item["ManufacturerID"].Text);
                HCDataContext db = new HCDataContext();
                Manufacturer m = C.ManufacturerController.GetByID(id, db);
                TextBox txtName = item.FindControl("txtName") as TextBox;
                TextBox txtWebsite = item.FindControl("txtWebsite") as TextBox;
                TextBox txtOrderDisplay = item.FindControl("txtOrderDisplay") as TextBox;
                FileUpload fileUploadLogoUrl = item.FindControl("fileUploadLogoUrl") as FileUpload;

                m.LogoUrl = fileUploadLogoUrl.HasFile ? M.FileM.UploadAndReturnURL(fileUploadLogoUrl, "/Product/Admin/ManufacturerManage/Image/") : m.LogoUrl;
                m.Name = txtName.Text;
                m.OrderDisplay = int.Parse(txtOrderDisplay.Text);
                m.Website = txtWebsite.Text;

                C.ManufacturerController.Update(m, db);
            }
        }
    }
}